package org.zjvis.datascience.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.zjvis.datascience.common.dto.DatabaseDTO;
import org.zjvis.datascience.service.mapper.DatabaseMapper;

import static org.zjvis.datascience.common.constant.DatabaseConstant.*;

/**
 * @description Database 数据源 Service 【多源异构做准备】
 * @date 2021-09-17
 */
@Service
public class DatabaseService {

    @Autowired
    private DatabaseMapper databaseMapper;

    @Value("${postgres.host}")
    private String default_address;

    @Value("${postgres.port}")
    private String default_port;

    @Value("${postgres.username}")
    private String default_username;

    @Value("${postgres.password}")
    private String default_password;

    @Value("${postgres.database}")
    private String default_db_name;

    @Value("${postgres.driver}")
    private String default_db_driver;

    public DatabaseDTO queryById(Long databaseId) {
        if (databaseId.equals(DEFAULT_DATASET_ID)){
            JSONObject configJson = new JSONObject();
            configJson.put("driver", default_db_driver);
            configJson.put("address", default_address);
            configJson.put("port", default_port);
            configJson.put("db", default_db_name);
            configJson.put("username", default_username);
            configJson.put("password", default_password);
            configJson.put("schema", GREEN_PLUM_DEFAULT_SCHEMA);
            return new DatabaseDTO().getDefaultDB(configJson);
        }else {
            return databaseMapper.queryById(databaseId);
        }
    }

}
